package jfoo;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import database.Database;

public class PreferencesDialog extends JDialog {

	Window window;
	JLabel labelLibrary;

	public PreferencesDialog(Window window) {
		this.window = window;
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		final JTextField LastFmLogin = new JTextField(Preferences.getInstance()
				.getLastFmLogin());
		LastFmLogin.setName("Login");
		// LastFmLogin.set
		final JPasswordField LastFmPassword = new JPasswordField(Preferences
				.getInstance().getLastFmPassword());

		JButton Save = new JButton("Save");
		Save.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Preferences p = Preferences.getInstance();
				p.setValue("lastFmLogin", LastFmLogin.getText());
				p.setValue("lastFmPassword", LastFmPassword.getText());
				p.setValue("libraryFolder", labelLibrary.getText());
				PreferencesDialog.this.dispose();
				Database.getInstance().scanLibrary();

			}
		});

		JButton Cancel = new JButton("Cancel");

		Cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				PreferencesDialog.this.dispose();

			}
		});
		this.setLayout(new GridLayout(5, 2));
		this.setLocation(
				(int) (window.getX() + window.getSize().getWidth() / 2),
				(int) (window.getX() + window.getSize().getHeight() / 2));

		this.add(new JLabel("Last.Fm Login : "));
		this.add(LastFmLogin);
		this.add(new JLabel("Last.Fm Password : "));
		this.add(LastFmPassword);
		
		
		 labelLibrary = new JLabel();
		final String library = Preferences.getInstance().getLibraryFolder();
		if (library.equals("")){
			labelLibrary.setText("No library directory selected");
		}else{
			labelLibrary.setText(library);
		}
		this.add(labelLibrary);
		JButton choose = new JButton("Choose");
		choose.addActionListener(new ActionListener() {
			

			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser(); 
				File f = new File(library);
			    chooser.setCurrentDirectory(f);
			    chooser.setDialogTitle("Select your Music folder");
			    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			    //
			    // disable the "All files" option.
			    //
			    chooser.setAcceptAllFileFilterUsed(false);
			    //    
			    if (chooser.showOpenDialog(PreferencesDialog.this) == JFileChooser.APPROVE_OPTION) { 
			      System.out.println("getCurrentDirectory(): " 
			         +  chooser.getCurrentDirectory());
			      System.out.println("getSelectedFile() : " 
			         +  chooser.getSelectedFile());
			      PreferencesDialog.this.labelLibrary.setText(chooser.getSelectedFile().toString());
			      }
			    else {
			    //	PreferencesDialog.this.labelLibrary.setText("No library directory selected");
			      }
				
			}
		});
	    
		this.add(choose);
		
		JLabel clear = new JLabel("Clear database ?");
		JButton ok = new JButton("Clear");
		this.add(clear);
		this.add(ok);
		
		this.add(Save);
		this.add(Cancel);
		this.setSize(450, 150);
		// this.pack();

		this.setVisible(true);
	}

}
